package com.sixteengroup.personnelms.controller.normalstaff;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.sixteengroup.personnelms.bean.RegUser;
import com.sixteengroup.personnelms.bean.log.CultivateLog;
import com.sixteengroup.personnelms.bean.to.Cultivate;
import com.sixteengroup.personnelms.bean.to.RegSalary;
import com.sixteengroup.personnelms.service.admin.LogService;
import com.sixteengroup.personnelms.service.normalstaff.RegService;
import com.sixteengroup.personnelms.service.portal.UserService;
import com.sixteengroup.personnelms.util.JWTutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/RegisterUser")
public class RegisterUserController {


    @Autowired
    UserService userService;
    @Autowired
    RegService regService;
    @Autowired
    LogService logService;
    @Autowired
    CultivateLog cultivateLog;

    //
    @PostMapping("/findUserList")
    public Map<String, Object> findMenuList(@RequestHeader("token") String token) {
        Map<String, Object> map = new HashMap<>();
        //从token中获取用户信息
        DecodedJWT tokenInfo = JWTutil.getTokenInfo(token);
        int id = tokenInfo.getClaim("id").asInt();
//        int type = tokenInfo.getClaim("type").asInt();


        RegUser regUser = regService.findUserList(id);
        System.out.println(regUser);
        try {
            map.put("code", 200);
            map.put("regUser", regUser);
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "服务器错误！");
        }
        return map;
    }


    @PostMapping("/getSararylimit")
    public HashMap<String, Object> getSararylimit(@RequestHeader("token") String token) {
        HashMap<String, Object> map = new HashMap<>();
        //从token中获取用户信息
        DecodedJWT tokenInfo = JWTutil.getTokenInfo(token);
        int id = tokenInfo.getClaim("id").asInt();

        List<RegSalary> list = regService.getSararylimit(id);
        System.out.println(list.toString());
        map.put("data", list);
        map.put("code", "200");

        return map;
    }

    @PostMapping("/updateRegUser")
    public String getSararylimit(@RequestHeader("token") String token,
                                 @RequestBody RegUser RegUser) {
        try {
            regService.updateUserInfo(RegUser);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }
    }

    //查询培训情况
    @PostMapping("/getCulvate")
    public HashMap<String, Object> getCulvate(@RequestHeader("token") String token) {
        System.err.println("---=-=-=-=-");
        HashMap<String, Object> map = new HashMap<>();
        //从token中获取用户信息
        DecodedJWT tokenInfo = JWTutil.getTokenInfo(token);
        int id = tokenInfo.getClaim("id").asInt();
        System.out.println(id);
        List<Cultivate> list = regService.getCulvate(id);
        System.out.println(list.toString());
        map.put("data", list);
        map.put("code", "200");

        return map;
    }


    @GetMapping("updateStatus")
    public String KillUser(Integer rowid, String rowstatus,@RequestHeader("token") String token) {
        System.out.println(rowid + rowstatus);
        cultivateLog.setStatus(rowstatus);
        cultivateLog.setPlanId(rowid);
        DecodedJWT tokenInfo = JWTutil.getTokenInfo(token);
        int id = tokenInfo.getClaim("id").asInt();
        cultivateLog.setUserId(id);
        try {
            if (rowstatus.equals("已完成")) {
                rowstatus = "已完成";
                //logService.savecultivate(cultivateLog);   //保存培训日志
                System.out.println("aaaaaa");
            } else {
                rowstatus = "未完成";
                System.out.println("bbbbb");
            }
            regService.killArticle(rowid, rowstatus);
            return rowstatus;
        } catch (Exception e) {
            return "0";
        }
    }

}
